package com.edu.advice;

import cn.hutool.core.bean.BeanUtil;
import com.edu.pojo.APIRequest;
import com.github.pagehelper.PageHelper;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * 用于拦截@EnableLimitPage注解，并且进行分页
 * @作者 five-five
 * @创建时间 2021/1/14
 */
@Component
@Aspect
public class LimitPageAdvice {
    @Around(value = "@annotation(com.edu.annotation.EnableLimitPage)")
    public Object limitPageAround(ProceedingJoinPoint joinPoint){
        Object proceed = null;
        //做分页操作
        try {
            //首先获得目标传过来的APIRequest参数
            APIRequest apiRequest = null;
            for (Object arg : joinPoint.getArgs()) {
                if (arg instanceof APIRequest) {
                    apiRequest = (APIRequest) arg;
                }
            }
            //没找到APIRequest的话，就不进行分页操作
            if (BeanUtil.isEmpty(apiRequest)) {
                return joinPoint.proceed();
            }
            PageHelper.startPage(apiRequest.getApiRequestPage().getPageNo(),apiRequest.getApiRequestPage().getLimit());
            proceed = joinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return proceed;
    }
}
